package cn.iftrue.japidemo.common;

import java.util.HashMap;

public class ApiResult extends HashMap<String,Object>{
    private static final String CODE="code";
    private static final String MSG="msg";
    private static final String DATA="data";
    private static final String SUCCESS_CODE="000000";
    private static final String SUCCESS_MSG="一切 ok";

    public static ApiResult success(){
        return successAddMsg(SUCCESS_MSG);
    }

    public static ApiResult successAddMsg(String msg){
        return success(msg,null);
    }

    public static ApiResult successAddData(Object data){
        return success(SUCCESS_MSG,data);
    }

    public static ApiResult success(String msg,Object data){
        if(null==msg){
            msg=SUCCESS_MSG;
        }
        return getResult(SUCCESS_CODE,msg,data);
    }

    public static ApiResult error(ApiErrorCode apiErrorCode){
        return error(apiErrorCode,null);
    }

    public static ApiResult error(ApiErrorCode apiErrorCode,Object data){
        return getResult(apiErrorCode.toString(),apiErrorCode.getMsg(),data);
    }

    private static ApiResult getResult (String code,String msg,Object data){
        ApiResult apiResult = new ApiResult();
        apiResult.put(CODE,code);
        apiResult.put(MSG,msg);
        if(null!=data) {
            apiResult.put(DATA, data);
        }
        return apiResult;
    }
}

